//
//  UIImage+circleImage.m
//  BaiSi
//
//  Created by mac on 16/10/18.
//  Copyright © 2016年 DeYi. All rights reserved.
//

#import "UIImage+circleImage.h"

@implementation UIImage (circleImage)

//将一张原始图片裁剪成圆形图片
+ (UIImage *)circleImageWithImage:(UIImage *)image {
    
    //判断图片是否为空
    
//    if (image == nil) {
//        //创建异常对象
//        NSException *exc =  [NSException exceptionWithName:@"ImageCanNotBeNill" reason:@"Image is nill" userInfo:nil];
//        //抛出异常
//        [exc raise];
//        return image;
//    }
    
    //1.开启图形上下文
    //scale:比例因子,像素与点的比例
    UIGraphicsBeginImageContextWithOptions(image.size, NO, 0);
    //2.描述圆形裁剪区域
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
    //3.添加裁剪区域
    [path addClip];
    //4.将图片画到上下文中
    [image drawAtPoint:CGPointZero];
    //5.获取图片
    image = UIGraphicsGetImageFromCurrentImageContext();
    //6.关闭上下文
    UIGraphicsEndImageContext();
    
    return image;
}

@end
